//
// Source code recreated from a .class file by IntelliJ IDEA
// (powered by FernFlower decompiler)
//

package io.gitee.caoxiaoyu97.util;


import io.gitee.caoxiaoyu97.entity.DynamicFileDTO;
import io.gitee.caoxiaoyu97.entity.ExcelModelFieldDTO;
import io.gitee.caoxiaoyu97.excel.MyEasyExcelConverter;
import javafx.scene.input.DataFormat;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.ObjectUtils;

import java.util.ArrayList;
import java.util.List;

@Slf4j
public class DynamicClassUtil {

    private static final String PACKAGE_NAME = "io.gitee.caoxiaoyu97.dynamic";

    private static final Class CONVERT_CLASS = MyEasyExcelConverter.class;


    public static String getModelCode(List<ExcelModelFieldDTO> list) {
        if (ObjectUtils.isEmpty(list)) {
            throw new RuntimeException("构造Model类上下文失败，总段列表为空");
        }

        String simpleClassName = getSimpleClassName(list);
        String packageName = "package " + PACKAGE_NAME + ";";
        List<String> imports = new ArrayList<>();
        imports.add("import com.alibaba.excel.annotation.ExcelProperty;");
        imports.add("import com.alibaba.excel.annotation.ExcelIgnoreUnannotated;");
        imports.add("import " + DataFormat.class.getName() + ";");
        imports.add("import " + CONVERT_CLASS.getName() + ";");
        imports.add("import net.sf.oval.constraint.*;");
        imports.add("import java.io.Serializable;");
        list.forEach(entity ->{
            if (ObjectUtils.isNotEmpty(entity.getImports())){
                imports.add(entity.getImports());
            }
        });
        String classDefinition = "public class " + simpleClassName + " implements Serializable {";
        StringBuffer sb = (new StringBuffer(packageName)).append("\n");
        imports.forEach((imp) -> {
            sb.append(imp).append("\n");
        });
        sb.append("@ExcelIgnoreUnannotated").append("\n");
        sb.append(classDefinition).append("\n");
        sb.append("  private static final long serialVersionUID = 1L;").append("\n");
        list.forEach((entity) -> {
            if (ObjectUtils.isEmpty(entity.getExportFlag()) || "Y".equals(entity.getExportFlag())) {
                sb.append(" @ExcelProperty(value = \"" + entity.getFiledName() + "\", index = " + entity.getFieldIndex() + ",converter = " + CONVERT_CLASS.getSimpleName() + ".class" +")").append("\n");
            }
            if (ObjectUtils.isNotEmpty(entity.getAnotations())){
                sb.append(entity.getAnotations()).append("\n");
            }
            sb.append(" private String " + entity.getFieldId() + ";").append("\n\n");
        });
        list.forEach((entity) -> {
            String fieldId = entity.getFieldId();
            String getStr = upperFirstAndAddPre(fieldId, "get");
            sb.append("  public String ").append(getStr).append("() {").append("\n");
            sb.append("   return ").append(fieldId).append(";").append("\n").append("}").append("\n\n");
            String setStr = upperFirstAndAddPre(fieldId, "set");
            sb.append("  public void ").append(setStr).append("(String " + fieldId + ") {").append("\n");
            sb.append("this." + fieldId + " = " + fieldId + ";").append("\n");
            sb.append("}").append("\n\n");
        });
        sb.append("}");
        log.info("动态类创建成功:{}", sb);
        return sb.toString();
    }

    private static String upperFirstAndAddPre(CharSequence str, String preString) {
        return str != null && preString != null ? preString + upperFirst(str) : null;
    }

    private static String upperFirst(CharSequence str) {
        if (null == str) {
            return null;
        } else {
            if (str.length() > 0) {
                char firstChar = str.charAt(0);
                if (Character.isLowerCase(firstChar)) {
                    return Character.toUpperCase(firstChar) + str.toString().substring(1);
                }
            }

            return str.toString();
        }
    }

    private static String getSimpleClassName(List<ExcelModelFieldDTO> list) {
        return getSimpleClassName(list.get(0));
    }

    private static String getSimpleClassName(DynamicFileDTO dto) {
        if (!ObjectUtils.isEmpty(dto.getVersion())) {
            String version = SpecailCharUtil.filterSpecialChar(dto.getVersion());
            return dto.getModelId() + version;
        } else {
            return dto.getModelId();
        }
    }

    public static String getClassName(DynamicFileDTO dto) {
        return PACKAGE_NAME + "." + getSimpleClassName(dto);
    }
}
